import { createTransport, Transporter } from 'nodemailer'
import { DependencyContainer } from 'tsyringe';
import { ROOT } from '@sker/core';
import { join } from 'path';
export interface EmailHandler<T = any> {
    (conn: Transporter): Promise<T>;
}
export const useEmail = async <T>(injector: DependencyContainer, handler: EmailHandler<T>): Promise<T> => {
    const root = injector.resolve(ROOT)
    const jsonFile = join(`${root}`, 'data/plugins', `@sker/email.json`)
    const config = require(jsonFile)
    const transport = createTransport({
        host: config.host,
        port: config.port,
        secure: true,
        auth: {
            user: config.user,
            pass: config.pass
        }
    })
    const res = await handler(transport);
    transport.close();
    return res;
}
export default useEmail;
